// pages/workTask/workTask.js

const app = getApp()
const lbbService = require('../service/service.js');
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight:0,
    taskData:[
    ],
    deleteUseItem:{},
    userId:"",
    show:false,
    nowItem:{},
    error:false,
    errorMsg:"请等待负责人完成后再进行操作",
    dialogDesc:"是否完成当前任务",
    buttons: [
      {
          type: 'default',
          className: '',
          text: '取消',
          value: 0
      },
      {
          type: 'primary',
          className: '',
          text: '确定',
          value: 1
      }
  ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.PageShow()
  },
  PageShow(){
    var that=this
    lbbService.getWorkTaskLogList({},res=>{
      var data=res.data.data
      var userId=res.data.userId
      lbbService.getAllInnerUser({}, ress => {
        var userList=ress.data
      for (let index = 0; index < data.length; index++) {
        var personNames=that.updateShowPersons(userList,data[index].chargePersonId,data[index].inspectPersonId);
        data[index].chargePersonName=personNames[0]
        data[index].inspectPersonName=personNames[1]
        // var cronMessage=that.CronToMessage(data[index].cron);
        // data[index].cronMessage=cronMessage
        let reg = /\//g;
        data[index].create=util.formatTime(new Date(data[index].create)).replace(reg, '-');
      }
      that.setData({
        taskData:data,
        userId:userId
      })
    })
    })
    that.setData({
      scrollHeight: app.getScrollHeight(45),
    })
  },
  showDetail:function(e){
    let item = e.currentTarget.dataset.item
    if (item.chargePersonId.indexOf(this.data.userId)!=-1&&item.state==2){
      this.setData({
        show:true,
        dialogDesc:"是否完成当前任务",
        nowItem:item
      })
    }
    else if(item.inspectPersonId.indexOf(this.data.userId)!=-1&&item.state==1){
      this.setData({
        show:true,
        dialogDesc:"是否已检查当前任务",
        nowItem:item
      })
    }
 
  },
  buttontap:function(e){
    var value=e.detail.item.value
    var item=this.data.nowItem
    var userId=this.data.userId
    if(value==0){
      this.setData({
        show:false
      })
    }
    else{
      let reg = /\//g;
      var time=util.formatTime(new Date(new Date())).replace(reg, '-');
      console.log(item)
      
        //完成当前任务
        if (item.chargePersonId.indexOf(userId)!=-1&&item.state==2){
          item.state=1
          item.completeUid=userId
          item.completeTime=time
          item.inspectCompleteUid=0
          item.inspectCompleteTime=time
          lbbService.completeTaskLog(item,res=>{
            this.setData({
              show:false
            })
            this.PageShow()
          })
        }
        else if(item.inspectPersonId.indexOf(userId)!=-1&&item.state==1){
            item.state=0
            item.inspectCompleteUid=userId
            item.completeTime=item.completeTime.replace("T", ' ');
            item.inspectCompleteTime=time
            lbbService.inspectTaskLog(item,res=>{
              this.setData({
                show:false
              })
              this.PageShow()
            })
        }
    // else if(item.inspectPersonId.indexOf(userId)!=-1&&item.state==2){
    //   this.setData({
    //     error:true,
    //     show:false
    //   })
    // }
  }
  },
  insertTask:function(){
    wx.navigateTo({
      url: '/pages/workTaskDetail/workTaskDetail',
    })
  },
  updateShowPersons(usersList,personIds,checkids){
    var personList=[]
    var personNames=""
    var checkPersons=""
    // lbbService.getAllInnerUser({}, res => {
      var idsList=personIds.split(",");
      for (let index = 0; index < idsList.length; index++) {
      for (let j = 0; j < usersList.length; j++) {
          if(usersList[j].uid==idsList[index]){
            if(idsList.length-1==index){
              personNames=personNames+usersList[j].userName
            }
            else {
              personNames=personNames+usersList[j].userName+","
            }
          }
        }
      
      }
      var checkidsList=checkids.split(",");
      for (let index = 0; index < checkidsList.length; index++) {
        for (let j = 0; j < usersList.length; j++) {
            if(usersList[j].uid==checkidsList[index]){
              if(checkidsList.length-1==index){
                checkPersons=checkPersons+usersList[j].userName
              }
              else {
                checkPersons=checkPersons+usersList[j].userName+","
              }
            }
          }
        
        }
        personList.push(personNames)
        personList.push(checkPersons)
      return personList
  // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.PageShow()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})